-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
17-H0ngJu #195
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์.. G ๋ฅผ R ๋ก ๋ฐ๊ฟ ์๊ฐ์ ๋ชปํ๊ณ bfs ์ฝ๋๋ฅผ ๋๊ฐ ์ง๋ฒ๋ ธ๋ค์..ใ
ใ
ํ์ฃผ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ค์ ์์ฑํด๋ดค์ต๋๋ค..!
์ฝ๋ ๊ธธ์ด๋ ์๊ฐ๋ ๋ ์ค์๋ค์!
import java.io.BufferedReader
import java.io.InputStreamReader
private lateinit var visited: Array<Array<Boolean>>
private lateinit var graph: Array<Array<String>>
private val dxdy = listOf((0 to 1), (0 to -1), (1 to 0), (-1 to 0))
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val n = br.readLine().toInt()
graph = Array(n) { Array(n) { "" } }
repeat(n) {
val line = br.readLine().chunked(1)
line.forEachIndexed { index, value -> graph[it][index] = value }
}
visited = Array(n) { Array(n) { false } }
var count = 0
var countRG = 0
for (x in 0 until n) {
for (y in 0 until n) {
if (bfs(x, y, n)) count++
}
}
visited = Array(n) { Array(n) { false } }
for (x in 0 until n) {
for (y in 0 until n) {
if (bfsRG(x, y, n)) countRG++
}
}
println("$count $countRG")
}
private fun bfs(x: Int, y: Int, n: Int): Boolean {
if (visited[x][y]) {
return false
}
val q = ArrayDeque<Pair<Int, Int>>()
q.add(x to y)
val color = graph[x][y]
while (q.isNotEmpty()) {
val now = q.removeFirst()
for (i in 0..3) {
val nextX = now.first + dxdy[i].first
val nextY = now.second + dxdy[i].second
if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0) {
continue
}
val nextColor = graph[nextX][nextY]
if (visited[nextX][nextY]) {
continue
}
if (color != nextColor) {
continue
}
visited[nextX][nextY] = true
q.add(nextX to nextY)
}
}
return true
}
private fun bfsRG(x: Int, y: Int, n: Int): Boolean {
if (visited[x][y]) {
return false
}
val q = ArrayDeque<Pair<Int, Int>>()
q.add(x to y)
val color = graph[x][y]
while (q.isNotEmpty()) {
val now = q.removeFirst()
for (i in 0..3) {
val nextX = now.first + dxdy[i].first
val nextY = now.second + dxdy[i].second
if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0) {
continue
}
val nextColor = graph[nextX][nextY]
if (visited[nextX][nextY]) {
continue
}
if (color != nextColor && (color == "B" || nextColor == "B")) {
continue
}
visited[nextX][nextY] = true
q.add(nextX to nextY)
}
}
return true
}
G ๋ฅผ R ๋ก ๋ฐ๊พธ๊ณ ์คํ : bfs ์ฝ๋๋ ํ๊ฐ
import java.io.BufferedReader
import java.io.InputStreamReader
private lateinit var visited: Array<Array<Boolean>>
private lateinit var graph: Array<Array<String>>
private val dxdy = listOf((0 to 1), (0 to -1), (1 to 0), (-1 to 0))
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val n = br.readLine().toInt()
graph = Array(n) { Array(n) { "" } }
repeat(n) {
val line = br.readLine().chunked(1)
line.forEachIndexed { index, value -> graph[it][index] = value }
}
visited = Array(n) { Array(n) { false } }
var count = 0
for (x in 0 until n) {
for (y in 0 until n) {
if (bfs(x, y, n)) count++
}
}
for (x in 0 until n) {
for (y in 0 until n) {
if (graph[x][y] == "G") {
graph[x][y] = "R"
}
}
}
var countRG = 0
visited = Array(n) { Array(n) { false } }
for (x in 0 until n) {
for (y in 0 until n) {
if (bfs(x, y, n)) countRG++
}
}
println("$count $countRG")
}
private fun bfs(x: Int, y: Int, n: Int): Boolean {
if (visited[x][y]) {
return false
}
val q = ArrayDeque<Pair<Int, Int>>()
q.add(x to y)
val color = graph[x][y]
while (q.isNotEmpty()) {
val now = q.removeFirst()
for (i in 0..3) {
val nextX = now.first + dxdy[i].first
val nextY = now.second + dxdy[i].second
if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0) {
continue
}
val nextColor = graph[nextX][nextY]
if (visited[nextX][nextY]) {
continue
}
if (color != nextColor) {
continue
}
visited[nextX][nextY] = true
q.add(nextX to nextY)
}
}
return true
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๊ฒฝ์ฐ์ 1. ์ผ๋ฐ์ธ์ฉ ๋ณด๋ 2. ์ ๋ก์์ฝ์ฉ ๋ณด๋ 2๊ฐ์ง๋ฅผ ๋ง๋ค์ด์ ๊ฐ ๋ณด๋์ ๋ํด dfs๋ฅผ ์ํํ๋ ์์ผ๋ก ํ์์๋ค์ :)
์ ๋ก์์ฝ์ผ ๊ฒฝ์ฐ R์ด๋ G๋ฅผ ๋ค๋ฅธ ํ์ชฝ์ผ๋ก ํต์ผ์ํค๋ ๊ฒ ์ด ๋ฌธ์ ๋ฅผ ํธ๋ key๋ผ๊ณ ์๊ฐ๋ฉ๋๋ค :)
#include <iostream>
#include <vector>
using namespace std;
int N;
void dfs(vector<vector<char>> &grid, int x, int y, char color) {
if (x < 0 || x >= N || y < 0 || y >= N) {
return;
}
if (grid[x][y] != color) {
return;
}
grid[x][y] = 'A';
dfs(grid, x - 1, y, color);
dfs(grid, x, y + 1, color);
dfs(grid, x + 1, y, color);
dfs(grid, x, y - 1, color);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
vector<vector<char>> grid1, grid2;
grid1.resize(N, vector<char>(N));
grid2.resize(N, vector<char>(N));
char ch;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> ch;
grid1[i][j] = ch;
if (ch == 'R') {
ch = 'G';
}
grid2[i][j] = ch;
}
}
int count1 = 0, count2 = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid1[i][j] != 'A') {
dfs(grid1, i, j, grid1[i][j]);
count1 += 1;
}
if (grid2[i][j] != 'A') {
dfs(grid2, i, j, grid2[i][j]);
count2 += 1;
}
}
}
cout << count1 << " " << count2;
return 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BFS/DFS๋ก ํ๋ฆด ๊ฑฐ ์๊ณ ์์๋๋ฐ ์ ๋์จ ํ์ธ๋๋ก๋ ํ๋ฆด ๊ฒ ๊ฐ์์ ํธ๋ผ์ด ํด๋ดค๋๋ฐ ์ ์๋๋ ์ง ๋ชจ๋ฅด๊ฒ ๋ค์ .......... ๋์์ฃผ์ธ์ ใ ใ ใ ใ ใ ใ ใ
from collections import Counter
import sys
def input(): return sys.stdin.readline().rstrip()
N = int(input())
board = []
def find_parent(graph, element):
if graph[element] == element: return element
parent = graph[element]
graph[element] = find_parent(graph, parent)
return graph[element]
def union(graph, first, second):
x = find_parent(graph, first)
y = find_parent(graph, second)
if x == y: return
x, y = min(x, y), max(x, y)
graph[y] = x
return
normal_parent = []
color_blindness_parent = []
idx = 0
for _ in range(N):
row = list(input())
for element in row:
normal_parent.append(idx)
color_blindness_parent.append(idx)
idx += 1
board.append(row)
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
for row in range(N):
for col in range(N):
for dir in range(4):
new_row = row + dy[dir]
new_col = col + dx[dir]
if new_row < 0 or new_row >= N: continue
if new_col < 0 or new_col >= N: continue
if board[row][col] == board[new_row][new_col]:
union(normal_parent, ((N * row) + col), ((N * new_row) + new_col))
if board[row][col] in ("R", "G") and board[new_row][new_col] in ("R", "G") or board[row][col] == board[new_row][new_col] == "B":
union(color_blindness_parent, (N * row + col), (N * new_row + new_col))
print(len(Counter(normal_parent).keys()), len(Counter(color_blindness_parent).keys()))
๊ฒฐ๊ตญ ์ ๋์จ ํ์ธ๋ ํฌ๊ธฐํ๊ณ BFS๋ก ํ์์๋๋ค..
from collections import deque
import sys
def input(): return sys.stdin.readline().rstrip()
N = int(input())
idx = 0
board = []
for _ in range(N):
board.append(list(input()))
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
visited = set()
deq = deque()
normal_answer = 0
for row in range(N):
for col in range(N):
if (row, col) in visited: continue
normal_answer += 1
visited.add((row, col))
deq.append((row, col))
while deq:
now_row, now_col = deq.popleft()
for dir in range(4):
new_row = now_row + dy[dir]
new_col = now_col + dx[dir]
if new_row < 0 or new_row >= N: continue
if new_col < 0 or new_col >= N: continue
if (new_row, new_col) in visited: continue
if board[row][col] == board[new_row][new_col]:
deq.append((new_row, new_col))
visited.add((new_row, new_col))
visited = set()
deq = deque()
color_blindness_answer = 0
for row in range(N):
for col in range(N):
if (row, col) in visited: continue
color_blindness_answer += 1
visited.add((row, col))
deq.append((row, col))
while deq:
now_row, now_col = deq.popleft()
for dir in range(4):
new_row = now_row + dy[dir]
new_col = now_col + dx[dir]
if new_row < 0 or new_row >= N: continue
if new_col < 0 or new_col >= N: continue
if (new_row, new_col) in visited: continue
if board[row][col] in ("R", "G") and board[new_row][new_col] in ("R", "G") or board[row][col] == board[new_row][new_col] == "B":
deq.append((new_row, new_col))
visited.add((new_row, new_col))
print(normal_answer, color_blindness_answer)
์ ๊ทผ๋ฐ R์ด๋ G ๋๊ฐ์ด ๋ง์ถ๊ณ ๋ค์ ๋๋ฆฌ๋ ๊ฑฐ ๋ค ์ ๋๋ค ๋ค ์ ๋
for i in range(N): | ||
for j in range(N): | ||
if arr[i][j] == "R": | ||
arr[i][j] = "G" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ R์ด๋ G ๋๊ฐ์ด ๋ง๋๋ ๊ฑฐ ๋ฏธ์ณค๋ค..
์ฝ๋๊ฐ ์กฐ๊ธ ๋๋ฝ๊ฒ ๋์๋ค์ ์ ๋... from collections import deque
import sys
dx = [0,0,-1,1]
dy = [1,-1,0,0]
input = sys.stdin.readline
n = int(input())
visited = [[False] * n for _ in range(n)]
graph = [list(input().rstrip()) for _ in range(n)]
queue = deque()
result = 0
def bfs():
global result
while queue:
x, y, color = queue.popleft()
for i in range(4):
nx, ny= x + dx[i], y + dy[i]
if nx < 0 or nx >= n or ny < 0 or ny >= n:
continue
if color == graph[ny][nx] and not visited[ny][nx] :
queue.append((nx, ny, color))
visited[ny][nx] = True
result += 1
def resultCount():
global result
for y in range(n):
for x in range(n):
if not visited[y][x] :
queue.append((x, y, graph[y][x]))
visited[y][x] = True
bfs()
print(result, end=" ")
result = 0
# ์ ๋ก์์ฝ ์๋๋ ์ถ๋ ฅ
resultCount()
# ์ ๋ก์์ฝ ์ ์ฉ ๋ถ๋ถ
for y in range(n):
for x in range(n):
visited[y][x] = False
if graph[y][x] == "G":
graph[y][x] = "R"
# ์ ๋ก์์ฝ ์ผ๋ ์ถ๋ ฅ
resultCount() |
bfs(i,j,arr[i][j]) | ||
result[0] += 1 | ||
|
||
visited = [[0] * N for _ in range(N)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
R,G์ ๋ค์ ๋ฐ๊พธ๋๊ฒ ๊ฐ์์ ํ์ธํด๋ณด๋ ์ด์งํผ ๋ชจ๋ graph๋ฅผ ๋๋๋ฐ
๊ทธ๋์ ์ ๋ ์ด๋ถ๋ถ์์ ๊ทธ๋ฅ visited๋ฅผ ์๋ก ๋ง๋ค์ง ์๊ณ False๋ก ์ด๊ธฐํ๋ฅผ ํด์คฌ์ต๋๋ค!
for y in range(n):
for x in range(n):
visited[y][x] = False
if graph[y][x] == "G":
graph[y][x] = "R"
for j in range(N): | ||
if not visited[i][j]: | ||
bfs(i,j,arr[i][j]) | ||
result[0] += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result ๋ฐฐ์ด ๋๊ฐ ๋ง๋ค์ด๋๊ณ ๊ฐ ๋ถ๋ถ์ ๋ง๊ฒ ์ถ๊ฐํ๋ ๋ถ๋ถ์ด ์์๊ตฐ์ ใทใทใทใทใทใทใท
์ ๋ ์ถ๋ ฅํ๊ณ ๋ค์ ์ด๊ธฐํ ํด์ฃผ๋ ํ์์ผ๋ก ํ๋๋ฐ ์ด ๋ฐฉ๋ฒ์ด ๋ ์ข์ ๊ฒ ๊ฐ๋ค์!
์ ๋์จํ์ธ๋ ์ฝ๋ ๋ฐ๋ก ์ฐพ์์ต๋๋ค
๋ฅผ ์ ๋ ฅํ๋ฉด ๋ต์ 11 6์ธ๋ฐ 12 7์ด ๋์ค๋ค์ฉ ํ ๊ทผ๋ฐ ์ ์๋๋์ง๋ ์ ๋ ์์ง ๋ชป์ฐพ๊ฒ ๋ค์.. ์ซ ๋ ์๊ฐํด๋ณด๊ฒ ์ด๋ฅ ======= for i in range(len(normal_parent)):
print(i, " : ", normal_parent[i]) ๋ฅผ ๋๋ฆฌ๋ฉด ์๋์ฒ๋ผ ๋์์ ๊ทธ๋ฐ๋ฐ ์ค์ ๋ก 361๋ฒ์งธ ๊ฐ์ ๋ณด๋ฉด ๋ถ๋ชจ๊ฐ 100์ด์ด์ผ ํ๋๋ฐ ๋ถ๋ชจ๊ฐ 360์ผ๋ก ๋์ด ์์ด์(0~400๋ฒ๊ธฐ์ค์ผ๋ก index๋ฅผ ์ก์ ๊ฒฝ์ฐ์ ๋๋ค) ๊ทธ๋์ print ์ถ๋ ฅํด์ ๋๋ ค๋ณด๋๊น ๊ทธ๋์ ์ต์ข print ๋ฌธ์ ์ถ๋ ฅํ๊ธฐ ์ ์, ํ๋ฒ๋ find_parent()๋ฅผ ๋๋ ค์ ๋ถ๋ชจ๋ ธ๋๋ฅผ ๊ฐ๋ฅดํค๋๋ก ๋ค์ ์ ๋ฐ์ดํธ ํด์ฃผ์ด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค.... ์ ๋ ต๋ค์ |
๋๋ฐ์ ์ด๋ค๋จ.. ์ํ์ฅฌ ๋๋ฐ... ์๊ฐํด๋ณด๋ ๋ง์ง๋ง์ find_parent() ๋๋ ค์ฃผ์ด์ ๋ณ๊ฒฝ๋ ์ง์ง ๋ถ๋ชจ๋ฅผ ์ฐพ์์ค์ผํด์ ์ ๋ ๊ฒ ํ๋ฌ ์๊ฐ์์ ์ธ์ ๋๋์?!?!? |
์์ ํต๊ณผ๋ฉ๋๋น ~~ |
๐ ๋ฌธ์ ๋งํฌ
์ ๋ก์์ฝ
โ๏ธ ์์๋ ์๊ฐ
1H 20M
โจ ์๋ ์ฝ๋
๋ฌธ์ ์์ฝ
์ ์์ธ๊ณผ, ์ ๋ น์์ฝ์ธ ์ฌ๋์ด ์ฃผ์ด์ง ๊ทธ๋ฆผ์ ๋ณด์์ ๋, ๊ฐ๊ฐ ๋ช ๊ฐ์ ๊ตฌ์ญ์ผ๋ก ๋ณด์ด๋์ง ๋ฐํํ๋ผ@!
bfs+๋ฐฑํธ๋ํน์ด๋ผ๊ณ ์๊ฐ์ ํ๊ณ
์ฃผ์ด์ง ๊ทธ๋ฆผ์์, R ๊ตฌ์ญ์ ์, G ๊ตฌ์ญ์ ์, B ๊ตฌ์ญ์ ์๋ฅผ ๊ฐ๊ฐ ๋ฐ๋ก ๊ตฌํ ๋ค์
์ ์์ธ์ด ๋ฐ๋ผ๋ณด๋ ๊ตฌ์ญ์ ๊ฐ์๋
R + G + B
,์ ๋ น์์ฝ์ธ ๊ฒฝ์ฐ
R + B
๋ก ๋จ์ํ๊ฒ ๊ตฌํ๋ ค๊ณ ํ์ต๋๋ค์ ์๊ณผ ๋ น์์ ๊ตฌ๋ถํ์ง ์์๋ ๋๋ฏ๋ก G๋ฅผ ์ ์ธ๋ฉด ๋๋ค๊ณ ์๊ฐํ์ต๋๋ค.
์ฆ, ์ฃผ์ด์ง ๊ทธ๋ฆผ์์ R์ G๋ก ๋ฐ๊พธ์ง ์์๋ ๊ฐ๋ฅํ ๊ฒ์ด๋ผ๊ณ ์์ํ์ต๋๋ค.
์ฝ๋๋ณด๊ธฐ
์ค ์ฌ์ด๋ฐ? ํ๊ณ ์ ์ถํ๋๋ 'ํ๋ ธ์ต๋๋ค'๊ฐ ๋ด์ต๋๋ค ใ ;;
์๋์ ๊ฒฝ์ฐ๊ฐ ๋ฐ๋ก์ ๋๋ค.
์ ์์ธ์ ๊ฒฝ์ฐ R = 2, G = 2, B = 0์ด๋ฏ๋ก, 4์ ๋๋ค.
๋จ์ํ ์ ๋ น์์ฝ์ธ ๊ฒฝ์ฐ R + B๋ก ๊ณ์ฐํ๊ฒ ๋๋ฉด, 2๊ฐ ๋์ต๋๋ค.
ํ์ง๋ง, ์ค์ ๋ก ์ ๋ น์์ฝ์ด ๋ฐ๋ผ๋ณด๋ ๊ตฌ์ญ์ ๊ฐ์๋ 1์ ๋๋ค.
R์ด๋ G ๋ฐ๊พธ์ง ์์๋ ์ ๋ น์์ฝ์ ๊ตฌ์ญ ์๋ฅผ ์ฐพ์ ์ ์์๊ฑฐ๋ผ๋ ์๊ฐ๊ณผ๋ ๋ฌ๋ฆฌ,
์์ ๊ฒฝ์ฐ์์ ๊ฒฐ๊ตญ R๊ณผ G๋ฅผ ๋ฐ๊ฟ์ค ํ, ๊ตฌ์ญ ์๋ฅผ ์ธ์ผํ๋ค๋ ๊ฒ์ ๊นจ๋ซ๊ณ ๋ก์ง์ ๊ณ ์ณค์ต๋๋ค !
์ต์ข ์๋ ์ฝ๋
0. ๋จผ์ R์ G๋ก ๋ฐ๊ฟ์ฃผ๊ณ ์ ์์ธ๊ณผ ๊ฐ์ ๋ก์ง ์ํ
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ